pimpas.ImportJS = Class.create( );
pimpas.Extends(pimpas.ImportJS, pimpas.Object);

pimpas.Extends(pimpas.ImportJS,
	{
		importScript: function(url, async) {
			if(url == null) throw new UrlNotFoundException();
			this.log.debug("[ImportJS]: Url => "+url);
			// create request
			var request = new pimpas.HTTPRequest( );
			// sync
			request.setAsync((async!=null&&async!=undefined) ? async : true);
			request.setUrl(url);
			request.setRequestMethod(pimpas.HTTPRequest.methods.GET);
			request.addParameter("date", new Date().getTime());
			request.addListener(pimpas.ImportJS.Listener);
			request.request( );
		}
	}
);

pimpas.ImportJS.Listener = Class.create( );
pimpas.Extends(pimpas.ImportJS.Listener, pimpas.HTTPRequest.Listener.prototype);
pimpas.Extends(pimpas.ImportJS.Listener,
	{
		callBack: function(obj, evt, text, xmlObject, xmlDocument) {
			switch(evt) {
				case pimpas.HTTPRequest.events.onComplete:
					// eval text
					try {
						eval(text);
					} catch(e) {
						throw new EvalScriptException(e);	
					}
				break;
				
				case pimpas.HTTPRequest.events.onError:
					// throw exception
					throw new ImportScriptException();
				break;
				case pimpas.HTTPRequest.events.onAbort:
					// throw exception
					throw new ImportScriptException();
				break;
			}
		}
	}
);

// exceptions
ImportScriptException = Class.create();
pimpas.Extends(ImportScriptException.prototype, pimpas.Object);
pimpas.Extends(ImportScriptException.prototype,
			   {
				   toString: function( ) {
						return "ImportScriptException";
				   }
			   }
);

EvalScriptException = Class.create();
pimpas.Extends(EvalScriptException.prototype, pimpas.Object);
pimpas.Extends(EvalScriptException.prototype,
			   {
				   ex: null,
				   initialize: function(e) {
						this.id=pimpas.globalID++;
						this.log = new pimpas.Log();
						this.ex = e;
				   },
				   toString: function( ) {
						return "EvalScriptException => "+((this.ex!=null) ? this.ex.message : "");
				   }
			   }
);
pimpas.ImportJS.initialize();